library(tidyverse) # for data cleaning and plotting
library(googlesheets4) # for reading googlesheet data
library(lubridate) # for date manipulation
library(openintro) # for the abbr2state() function
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
library(ggmap) # for mapping points on maps
library(gplots) # for col2hex() function
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
library(leaflet) # for highly customizable mapping
library(carData) # for Minneapolis police stops data
library(ggthemes) # for more themes (including theme_map())
gs4_deauth() # To not have to authorize each time you knit.
theme_set(theme_minimal())
# Starbucks locations
Starbucks <- read_csv("https://www.macalester.edu/~ajohns24/Data/Starbucks.csv")
starbucks_us_by_state <- Starbucks %>%
filter(Country == "US") %>%
count(`State/Province`) %>%
mutate(state_name = str_to_lower(abbr2state(`State/Province`)))
# Lisa's favorite St. Paul places - example for you to create your own data
favorite_stp_by_lisa <- tibble(
place = c("Home", "Macalester College", "Adams Spanish Immersion",
"Spirit Gymnastics", "Bama & Bapa", "Now Bikes",
"Dance Spectrum", "Pizza Luce", "Brunson's"),
long = c(-93.1405743, -93.1712321, -93.1451796,
-93.1650563, -93.1542883, -93.1696608,
-93.1393172, -93.1524256, -93.0753863),
lat = c(44.950576, 44.9378965, 44.9237914,
44.9654609, 44.9295072, 44.9436813,
44.9399922, 44.9468848, 44.9700727)
)
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
Instructions
Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
These exercises will reiterate what you learned in the “Mapping data with R” tutorial. If you haven’t gone through the tutorial yet, you should do that first.
ggmap)Starbucks locations to a world map. Add an aesthetic to the world map that sets the color of the points according to the ownership type. What, if anything, can you deduce from this visualization?From the map we can see that most of the Starbucks are concentrated in the East coast of North America, UK and East and Southeast of Asia. The map shows that most of the Starbucks in California are company owned. In South Korea and Japan, most of the Starbucks are joint ventured, while in China, many Starbucks are company owned.
# Get the map information
world <- get_stamenmap(
bbox = c(left = -180, bottom = -57, right = 179, top = 82.1),
maptype = "terrain",
zoom = 2)
# Plot the points on the map
ggmap(world) + # creates the map "background"
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color = `Ownership Type`),
alpha = .7,
size = .1) +
theme_map()
# Get the map information
Twin_Cities_map <- get_stamenmap(
bbox = c(left = -93.3707, bottom = 44.8719, right = -92.9965, top = 45.0580),
maptype = "terrain",
zoom = 12)
# Plot the points on the map
ggmap(Twin_Cities_map) + # creates the map "background"
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color= `Ownership Type`)) +
theme_map()
I change the zoom number from 12 to 10, and the background of the map becomes unclear, showing up less details.
Twin_Cities_map <- get_stamenmap(
bbox = c(left = -93.3707, bottom = 44.8719, right = -92.9965, top = 45.0580),
maptype = "terrain",
zoom = 10)
ggmap(Twin_Cities_map) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color= `Ownership Type`)) +
theme_map()
get_stamenmap() in help and look at maptype). Include a map with one of the other map types.Twin_Cities_map <- get_stamenmap(
bbox = c(left = -93.3707, bottom = 44.8719, right = -92.9965, top = 45.0580),
maptype = "toner",
zoom = 12)
ggmap(Twin_Cities_map) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color= `Ownership Type`)) +
theme_map()
annotate() function (see ggplot2 cheatsheet).Twin_Cities_map<-get_stamenmap(
bbox = c(left = -93.3351, bottom =44.8638, right =-92.9609 , top =45.0499),
maptype = "terrain",
zoom = 12)
ggmap(Twin_Cities_map) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color= `Ownership Type`)) +
annotate(geom = "point",
x = -93.1810,
y = 44.9320,
color = "Blue",
size = 2)+
annotate(geom = "text",
x =-93.1810,
y = 44.9320,
label = "Macalester College",
size = 4)
theme_map()
## List of 93
## $ line :List of 6
## ..$ colour : chr "black"
## ..$ size : num 0.409
## ..$ linetype : num 1
## ..$ lineend : chr "butt"
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ rect :List of 5
## ..$ fill : chr "white"
## ..$ colour : chr "black"
## ..$ size : num 0.409
## ..$ linetype : num 1
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ text :List of 11
## ..$ family : chr ""
## ..$ face : chr "plain"
## ..$ colour : chr "black"
## ..$ size : num 9
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : num 0
## ..$ lineheight : num 0.9
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ title : NULL
## $ aspect.ratio : NULL
## $ axis.title : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.title.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 2.25points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 2.25points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.x.bottom : NULL
## $ axis.title.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 2.25points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.title.y.left : NULL
## $ axis.title.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 2.25points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.text.x :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 1.8points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.top :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : num 0
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 1.8points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.x.bottom : NULL
## $ axis.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 1
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 1.8points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.text.y.left : NULL
## $ axis.text.y.right :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 0points 1.8points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ axis.ticks : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.ticks.x : NULL
## $ axis.ticks.x.top : NULL
## $ axis.ticks.x.bottom : NULL
## $ axis.ticks.y : NULL
## $ axis.ticks.y.left : NULL
## $ axis.ticks.y.right : NULL
## $ axis.ticks.length : 'simpleUnit' num 2.25points
## ..- attr(*, "unit")= int 8
## $ axis.ticks.length.x : NULL
## $ axis.ticks.length.x.top : NULL
## $ axis.ticks.length.x.bottom: NULL
## $ axis.ticks.length.y : NULL
## $ axis.ticks.length.y.left : NULL
## $ axis.ticks.length.y.right : NULL
## $ axis.line : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ axis.line.x : NULL
## $ axis.line.x.top : NULL
## $ axis.line.x.bottom : NULL
## $ axis.line.y : NULL
## $ axis.line.y.left : NULL
## $ axis.line.y.right : NULL
## $ legend.background :List of 5
## ..$ fill : NULL
## ..$ colour : logi NA
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ legend.margin : 'margin' num [1:4] 4.5points 4.5points 4.5points 4.5points
## ..- attr(*, "unit")= int 8
## $ legend.spacing : 'simpleUnit' num 9points
## ..- attr(*, "unit")= int 8
## $ legend.spacing.x : NULL
## $ legend.spacing.y : NULL
## $ legend.key :List of 5
## ..$ fill : chr "white"
## ..$ colour : logi NA
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ legend.key.size : 'simpleUnit' num 1.2lines
## ..- attr(*, "unit")= int 3
## $ legend.key.height : NULL
## $ legend.key.width : NULL
## $ legend.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.text.align : NULL
## $ legend.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ legend.title.align : NULL
## $ legend.position : num [1:2] 0 0
## $ legend.direction : NULL
## $ legend.justification : num [1:2] 0 0
## $ legend.box : NULL
## $ legend.box.just : NULL
## $ legend.box.margin : 'margin' num [1:4] 0cm 0cm 0cm 0cm
## ..- attr(*, "unit")= int 1
## $ legend.box.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ legend.box.spacing : 'simpleUnit' num 9points
## ..- attr(*, "unit")= int 8
## $ panel.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.border : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.spacing : 'simpleUnit' num 0lines
## ..- attr(*, "unit")= int 3
## $ panel.spacing.x : NULL
## $ panel.spacing.y : NULL
## $ panel.grid : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ panel.grid.major : NULL
## $ panel.grid.minor :List of 6
## ..$ colour : NULL
## ..$ size : 'rel' num 0.5
## ..$ linetype : NULL
## ..$ lineend : NULL
## ..$ arrow : logi FALSE
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_line" "element"
## $ panel.grid.major.x : NULL
## $ panel.grid.major.y : NULL
## $ panel.grid.minor.x : NULL
## $ panel.grid.minor.y : NULL
## $ panel.ontop : logi FALSE
## $ plot.background : list()
## ..- attr(*, "class")= chr [1:2] "element_blank" "element"
## $ plot.title :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 4.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.title.position : chr "panel"
## $ plot.subtitle :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : num 0
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 0points 0points 4.5points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 0.8
## ..$ hjust : num 1
## ..$ vjust : num 1
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 4.5points 0points 0points 0points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.caption.position : chr "panel"
## $ plot.tag :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : 'rel' num 1.2
## ..$ hjust : num 0.5
## ..$ vjust : num 0.5
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.tag.position : chr "topleft"
## $ plot.margin : 'margin' num [1:4] 4.5points 4.5points 4.5points 4.5points
## ..- attr(*, "unit")= int 8
## $ strip.background :List of 5
## ..$ fill : chr "grey85"
## ..$ colour : chr "grey20"
## ..$ size : NULL
## ..$ linetype : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_rect" "element"
## $ strip.background.x : NULL
## $ strip.background.y : NULL
## $ strip.placement : chr "inside"
## $ strip.text :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : chr "grey10"
## ..$ size : 'rel' num 0.8
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : 'margin' num [1:4] 3.6points 3.6points 3.6points 3.6points
## .. ..- attr(*, "unit")= int 8
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.text.x : NULL
## $ strip.text.y :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num -90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ strip.switch.pad.grid : 'simpleUnit' num 2.25points
## ..- attr(*, "unit")= int 8
## $ strip.switch.pad.wrap : 'simpleUnit' num 2.25points
## ..- attr(*, "unit")= int 8
## $ strip.text.y.left :List of 11
## ..$ family : NULL
## ..$ face : NULL
## ..$ colour : NULL
## ..$ size : NULL
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : num 90
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi TRUE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi TRUE
## - attr(*, "validate")= logi TRUE
geom_map())The example I showed in the tutorial did not account for population of each state in the map. In the code below, a new variable is created, starbucks_per_10000, that gives the number of Starbucks per 10,000 people. It is in the starbucks_with_2018_pop_est dataset.
census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
separate(state, into = c("dot","state"), extra = "merge") %>%
select(-dot) %>%
mutate(state = str_to_lower(state))
starbucks_with_2018_pop_est <-
starbucks_us_by_state %>%
left_join(census_pop_est_2018,
by = c("state_name" = "state")) %>%
mutate(starbucks_per_10000 = (n/est_pop_2018)*10000)
dplyr review: Look through the code above and describe what each line of code does.census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
#gets data from the csv file
separate(state, into = c("dot","state"), extra = "merge") %>% # separates "dot" and "state" variables, using extra and "merge" to split at most length times. Make sure that "sparate" function does not return "New York" as "New".
select(-dot) %>% # singles out the "dot" variable
mutate(state = str_to_lower(state))#changes state_name all to lower-case letters
starbucks_with_2018_pop_est <-
starbucks_us_by_state %>%
left_join(census_pop_est_2018,# combines census_pop_est_2018 and starbucks_us_by_state to create the dataset starbucks_with_2018_pop_est
by = c("state_name" = "state")) %>%
mutate(starbucks_per_10000 = (n/est_pop_2018)*10000)# adds a new column to the new dataset starbucks_with_2018_pop_est called "starbucks_per_10000", and starbucks_per_10000 = (n/est_pop_2018)*10000.
Conclusion: The map shows that the number of Starbucks per 10,000 people is higher in the East Coast and West Coast, but lower in Central. It also shows that California has the largest number of Starbucks in the U.S.
states_map <- map_data("state")
starbucks_in_us <- Starbucks %>%
filter(Country == "US",!(`State/Province` %in% c("AK", "HI")))%>%
select("State/Province","Longitude", "Latitude" ) %>%
mutate(state_name = str_to_lower(abbr2state(`State/Province`)))
starbucks_with_2018_pop_est %>%
ggplot() +
geom_map(map = states_map,
aes(map_id = state_name,
fill = n)) +
geom_point(data = starbucks_in_us,
aes(x = Longitude, y = Latitude),
size = .05,
alpha = .2,
color = "goldenrod") +
expand_limits(x = states_map$long, y = states_map$lat) +
labs(title = "The Number of Starbucks per 10,000 People in the US") +
theme_map() +
theme(legend.background = element_blank())
leaflet)tibble() function that has 10-15 rows of your favorite places. The columns will be the name of the location, the latitude, the longitude, and a column that indicates if it is in your top 3 favorite locations or not. For an example of how to use tibble(), look at the favorite_stp_by_lisa I created in the data R code chunk at the beginning.favorite_places <- tibble(
place = c("Apartment", "Macalester College", "Sencha",
"Kimchi Tofu House", "St. Paul Public Library", "Everest on Grand",
"Minnehaha Park", "Café Latte", "Kowalski","Whole Foods"),
long = c(-93.16524, -93.1810, -93.17350,
-93.22838, -93.09855, -93.15558,
-93.4187, -93.13760, -93.15436, -93.16819),
lat = c(44.93339, 44.9320, 44.93923,
44.97256, 44.94300, 44.93918,
44.9307, 44.93901, 44.93943, 44.94601),
top_three = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE,FALSE, FALSE,FALSE, FALSE)
)
leaflet map that uses circles to indicate your favorite places. Label them with the name of the place. Choose the base map you like best. Color your 3 favorite places differently than the ones that are not in your top 3 (HINT: colorFactor()). Add a legend that explains what the colors mean.pal <- colorFactor(palette(c("red", "blue")),
domain = favorite_places$top_three)
leaflet(data=favorite_places)%>%
addTiles()%>%
addProviderTiles(providers$CartoDB.DarkMatter)%>%
addCircles(lng = ~long,
lat = ~lat,
label = ~place,
weight = 10,
opacity = 1,
color = ~pal(top_three))%>%
addLegend("bottomleft",
pal = pal,
values = ~top_three,
opacity = 1)
pal <- colorFactor(palette(c("red", "blue")),
domain = favorite_places$top_three)
leaflet(data=favorite_places)%>%
addTiles()%>%
addProviderTiles(providers$CartoDB.DarkMatter)%>%
addCircles(lng = ~long,
lat = ~lat,
label = ~place,
weight = 10,
opacity = 1,
color = ~pal(top_three))%>%
addLegend("bottomleft",
pal = pal,
values = ~top_three,
opacity = 1)%>%
addPolylines(lng = ~long,
lat = ~lat,
color="white",
weight = 1)
This section will revisit some datasets we have used previously and bring in a mapping component.
The data come from Washington, DC and cover the last quarter of 2014.
Two data tables are available:
Trips contains records of individual rentalsStations gives the locations of the bike rental stationsHere is the code to read in the data. We do this a little differently than usualy, which is why it is included here rather than at the top of this file. To avoid repeatedly re-reading the files, start the data import chunk with {r cache = TRUE} rather than the usual {r}. This code reads in the large dataset right away.
data_site <-
"https://www.macalester.edu/~dshuman1/data/112/2014-Q4-Trips-History-Data.rds"
Trips <- readRDS(gzcon(url(data_site)))
Stations<-read_csv("http://www.macalester.edu/~dshuman1/data/112/DC-Stations.csv")
Stations to make a visualization of the total number of departures from each station in the Trips data. Use either color or size to show the variation in number of departures. This time, plot the points on top of a map. Use any of the mapping tools you’d like.Stations%>%
summarise(min(lat),
max(lat),
min(long),
max(long))
dc_map<-get_stamenmap(
bbox = c(left = -77.2025, bottom = 38.80111, right = -76.93186, top = 39.12351),
maptype = "terrain",
zoom = 11)
departure_by_station<-Trips%>%
left_join(Stations, by = c("sstation" = "name"))%>%
group_by(lat, long)%>%
summarise(n=n())
ggmap(dc_map)+
geom_point(data=departure_by_station,
aes(x=long,
y=lat,
color=n),
alpha=0.8,
shape=17)+
scale_color_viridis_c()
Stations with a high percentage of departures by casual users are concentrated in the area with longitude -77.05 and latitutde 38.9. The percentage of departures by casual users is lower in areas with higher latitude.
Stations%>%
summarise(min(lat),
max(lat),
min(long),
max(long))
dc_map<-get_stamenmap(
bbox = c(left = -77.2025, bottom = 38.80111, right = -76.93186, top = 39.12351),
maptype = "terrain",
zoom = 11)
departure_by_station<-Trips%>%
left_join(Stations, by = c("sstation" = "name"))%>%
group_by(lat, long)%>%
summarise(n=n(), prop_casual = mean(client == "Casual"))
ggmap(dc_map)+
geom_point(data=departure_by_station,
aes(x=long,
y=lat,
color=prop_casual),
alpha=0.8,
shape=17)+
scale_color_viridis_c()
The following exercises will use the COVID-19 data from the NYT.
The map shows the cumulative number of Covid19 in each state. From the graph, we can see that Texas and California have the most cumulative cases in the U.S.. The problem with this map is that it doesn’t show the relationship between the number of cumulative cases and the population density, given that each state has different population.
states_map<-map_data("state")
covid19%>%
group_by(state)%>%
summarise(total = max(cases))%>%
mutate(state = str_to_lower(state))%>%
ggplot()+
geom_map(map = states_map,
aes(map_id = state,
fill = total))+
expand_limits(x = states_map$long, y = states_map$lat)+
theme_map()
census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
separate(state, into = c("dot","state"), extra = "merge") %>%
select(-dot) %>%
mutate(state = str_to_lower(state))
covid19%>%
group_by(state)%>%
summarise(total=max(cases))%>%
mutate(state = str_to_lower(state))%>%
left_join(census_pop_est_2018,
by=c("state"="state"))%>%
mutate(num_cases_per_10000 = (total/est_pop_2018)*10000)%>%
ggplot()+
geom_map(map = states_map,
aes(map_id = state,
fill = num_cases_per_10000))+
expand_limits(x = states_map$long, y = states_map$lat)+
theme_map()
These exercises use the datasets MplsStops and MplsDemo from the carData library. Search for them in Help to find out more information.
MplsStops dataset to find out how many stops there were for each neighborhood and the proportion of stops that were for a suspicious vehicle or person. Sort the results from most to least number of stops. Save this as a dataset called mpls_suspicious and display the table.summary(MplsStops)
## idNum date problem
## 17-000003: 1 Min. :2017-01-01 00:00:42 suspicious:25822
## 17-000007: 1 1st Qu.:2017-03-29 08:35:09 traffic :26098
## 17-000073: 1 Median :2017-06-17 18:46:47
## 17-000092: 1 Mean :2017-06-23 19:57:49
## 17-000098: 1 3rd Qu.:2017-09-18 18:32:06
## 17-000111: 1 Max. :2017-12-31 23:52:35
## (Other) :51914
## MDC citationIssued personSearch vehicleSearch
## MDC :43699 NO :15899 NO :38462 NO :40579
## other: 8221 YES : 3211 YES : 5237 YES : 3120
## NA's:32810 NA's: 8221 NA's: 8221
##
##
##
##
## preRace race gender lat
## Unknown :28337 Black :15220 Female :10015 Min. :44.89
## Black : 6805 White :11703 Male :27131 1st Qu.:44.95
## White : 6004 Unknown : 9219 Unknown: 6492 Median :44.98
## Native American: 908 East African: 2188 NA's : 8282 Mean :44.97
## Latino : 528 Latino : 1858 3rd Qu.:45.00
## (Other) : 1117 (Other) : 3511 Max. :45.05
## NA's : 8221 NA's : 8221
## long policePrecinct neighborhood
## Min. :-93.33 Min. :1.000 Downtown West: 4409
## 1st Qu.:-93.29 1st Qu.:2.000 Whittier : 3328
## Median :-93.28 Median :3.000 Near - North : 2256
## Mean :-93.27 Mean :3.257 Lyndale : 2154
## 3rd Qu.:-93.25 3rd Qu.:4.000 Jordan : 2075
## Max. :-93.20 Max. :5.000 Hawthorne : 2031
## (Other) :35667
mpls_suspicious<-MplsStops%>%
group_by(neighborhood)%>%
summarise(number_stops = n(),
n_suspicious = sum(problem == "suspicious"),
prop_suspicious = n_suspicious/number_stops)%>%
arrange(desc(number_stops))
leaflet map and the MplsStops dataset to display each of the stops on a map as a small point. Color the points differently depending on whether they were for suspicious vehicle/person or a traffic stop (the problem variable). HINTS: use addCircleMarkers, set stroke = FAlSE, use colorFactor() to create a palette.pal_problem<-colorFactor("viridis",
domain = MplsStops$problem)
leaflet(MplsStops)%>%
addProviderTiles(providers$Stamen.Toner)%>%
addCircles(lng = ~long,
lat = ~lat,
color = ~pal_problem(problem),
weight = 0.5,
opacity = 0.4)%>%
addLegend(pal = pal_problem,
values = ~problem)
eval=FALSE. Although it looks like it only links to the .sph file, you need the entire folder of files to create the mpls_nbhd data set. These data contain information about the geometries of the Minneapolis neighborhoods. Using the mpls_nbhd dataset as the base file, join the mpls_suspicious and MplsDemo datasets to it by neighborhood (careful, they are named different things in the different files). Call this new dataset mpls_all.mpls_nbhd <- st_read("Minneapolis_Neighborhoods/Minneapolis_Neighborhoods.shp", quiet = TRUE)
mpls_all<-mpls_nbhd %>%
left_join(mpls_suspicious,
by = c("BDNAME" = "neighborhood"))%>%
left_join(MplsDemo,
by = c("BDNAME" = "neighborhood"))
leaflet to create a map from the mpls_all data that colors the neighborhoods by prop_suspicious. Display the neighborhood name as you scroll over it. Describe what you observe in the map.pal_sus<-colorNumeric("viridis",
domain = mpls_all$prop_suspicious)
leaflet(mpls_all)%>%
addProviderTiles(providers$Stamen.Toner)%>%
addPolygons(fillColor = ~pal_sus(prop_suspicious),
fillOpacity = 0.5,
label = ~BDNAME,
color = "black",
weight = 1,
opacity = 1)%>%
addLegend(pal = pal_sus,
values = ~prop_suspicious)
leaflet to create a map of your own choosing. Come up with a question you want to try to answer and use the map to help answer that question. Describe what your map shows. Question:Where do most of my favorite places concentrate? My map shows where my favorite places locate in Twin Cities. It also shows that most of my favorite places concentrate in St. Paul. Many of them are places around campus.pal <- colorFactor(palette(c("red", "blue")),
domain = favorite_places$top_three)
leaflet(data=favorite_places)%>%
addTiles()%>%
addProviderTiles(providers$CartoDB.Voyager)%>%
addCircles(lng = ~long,
lat = ~lat,
label = ~place,
weight = 10,
opacity = 1,
color = ~pal(top_three))%>%
addLegend("bottomleft",
pal = pal,
values = ~top_three,
opacity = 1)%>%
addPolylines(lng = ~long,
lat = ~lat,
color="white",
weight = 1)
GitHub link